package com.app.controller;

import com.app.model.AdminUserModel;
import com.app.model.AwardModel;
import com.app.model.PointOutModel;
import com.app.service.AdminUserService;
import com.app.service.AwardService;
import com.app.service.PointOutService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Controller
@RequestMapping("/api")
public class AwardBehindController {
    @Autowired
    private AwardService awardService;


    @Autowired
    private AdminUserService adminUserService;
    /**
     * 奖励
     * @return
     * @throws Exception
     */
    @GetMapping("/getBehindAwardList")
    public Result<Object> getBehindAwardList(HttpServletRequest request){
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        AwardModel model = awardService.getModel();
        return Result.success(model);
    }


    /**
     * 奖励编辑
     * @return
     * @throws Exception
     */
    @PostMapping("/editAward")
    public Result<Object> editAward(HttpServletRequest request,
                                       @RequestBody AwardModel model){
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        model.setUpTime(CommonHelp.getNowTimestamp().toString());
        awardService.update(model);
        return Result.success("操作成功");
    }

}
